package csel.model.items;

import csel.model.BlockNotifier;
import csel.model.Direction;
import csel.model.MobileObject;
import csel.model.entity.Entity;

public class Door extends Item
{
	Direction currentDirection;
	Direction closedDirection;
	Direction openDirection;
	public Door(String typeId)
	{
		super(typeId);
	}
	private boolean isLocked;
	public Door()
	{
		super("door");
		isLocked = false;
	}
	public void setDirection(Direction d)
	{

		currentDirection = d;
		if(closedDirection == null)
		{
			closedDirection = d;	
			if(closedDirection.equals(Direction.SOUTH))
			{
				openDirection = Direction.SOUTHWEST;
			}
			else if(closedDirection.equals(Direction.WEST))
			{
				openDirection = Direction.NORTHWEST;
			}
			else if(closedDirection.equals(Direction.NORTH))
			{
				openDirection = Direction.NORTHEAST;
			}
			else
			{
				openDirection = Direction.SOUTHEAST;
			}
		}
	}
	public Direction getDirection()
	{
		return(currentDirection);
	}
	public boolean canEnter(MobileObject obj, BlockNotifier notifier)
	{
		if(!isLocked && notifier != null)
		{
			notifier.blockedBy(this);
		}
		return(!isLocked);
	}
	protected void setLocked(boolean l)
	{
		this.isLocked = l;
	}
	public void toggleLocked()
	{
		isLocked = !isLocked;
	}
	@Override
	public boolean enter(Entity e)
	{
		openDoor();
		return false;
	}

	@Override
	public void leave(Entity e)
	{
		closeDoor();

	}
	public void openDoor()
	{
		setDirection(openDirection);
	}
	public void closeDoor()
	{
		setDirection(closedDirection);
	}
}
